package com.milo.datasource;

import com.alibaba.druid.util.DruidPasswordCallback;

import com.milo.common.utils.AESUtil;
import com.milo.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Properties;


/**
 * 多数据源密码解密
 * @author milogenius
 * @date 2020/4/21 15:27
 *
 */
@Component
@Slf4j
public class DbPasswordCallback extends DruidPasswordCallback {
    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        String password = properties.getProperty("password");
        if (StringUtils.isNotEmpty(password)) {
            try {
                //所以这里的代码是将密码进行解密
                String sourcePassword = AESUtil.aesDecode(password);
                log.info(sourcePassword);
                setPassword(sourcePassword.toCharArray());
            } catch (Exception e) {
                setPassword(password.toCharArray());
            }
        }
    }

}
